package socket.wtyt.com.wtytsocket;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    EditText contentEt;
    Button sendBtn;


    EditText ipEt;
    EditText portEt;
    Button connectBtn;
    TextView connectStateTv;

    //发送测试
    Button sendTestBtn;
    EditText sendTestEt;

    //全开、全关
    Button openAllBtn, closeAllBtn, clearLogBtn;
    EditText logEt;


//    send_test_btn

    String defIp, defPort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        ipEt = findViewById(R.id.ip_et);
        portEt = findViewById(R.id.port_et);
        connectBtn = findViewById(R.id.connect_btn);
        connectStateTv = findViewById(R.id.connect_state_tv);

        sendTestBtn = findViewById(R.id.send_test_btn);
        sendTestEt = findViewById(R.id.send_test_et);

        openAllBtn = findViewById(R.id.open_all_btn);
        closeAllBtn = findViewById(R.id.close_all_btn);
        clearLogBtn = findViewById(R.id.clear_log_btn);

        logEt = findViewById(R.id.log_et);

        //下面的不用了暂时
        contentEt = findViewById(R.id.content_et);
        sendBtn = findViewById(R.id.send_btn);

        connectBtn.setOnClickListener(this);
        sendTestBtn.setOnClickListener(this);
        openAllBtn.setOnClickListener(this);
        closeAllBtn.setOnClickListener(this);
        clearLogBtn.setOnClickListener(this);


        sendBtn.setOnClickListener(this);
    }

    private void initData() {
//        defIp = "192.168.2.104";

        defIp = Consts.DEF_SERVER_IP;
        defPort = Consts.DEF_SERVER_PORT;

        ipEt.setText(defIp);
        portEt.setText(defPort);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.connect_btn:
                onConnectClick();
                break;

            case R.id.send_test_btn:
                onSendTestClick();
                break;
            case R.id.open_all_btn:
                onOpenAllClick();
                break;

            case R.id.close_all_btn:
                onCloesAllClick();
                break;

            case R.id.clear_log_btn:
                logEt.setText("");
                break;

            case R.id.send_btn:
                break;
        }
    }

    /**
     * 测试连接
     */
    private void onConnectClick() {
        String ip = ipEt.getText().toString().trim();
        final String port = portEt.getText().toString().trim();

        if(TextUtils.isEmpty(ip)
                || TextUtils.isEmpty(port)) {
            toast("ip 或 端口号不能为空哦");
            return;
        }

//        new Thread(){
//            @Override
//            public void run() {
//                SocketClient.getSingle().getUDPConnect(Integer.valueOf(port), "测试udp", new CallBackSocketUDP() {
//                    @Override
//                    public void isConnect(boolean state) {
//
//                    }
//
//                    @Override
//                    public void Receive(String data) {
//
//                    }
//                });
//            }
//        }.start();


        SocketClient.getSingle().testConnect(ip, Integer.valueOf(port), new IConnectListener() {
            @Override
            public void isConnect(boolean state, String msg) {
                if(state) {
                    connectStateTv.setText(msg);
                    toast(msg);
                }else {
                    connectStateTv.setText("连接失败");
                    toast(msg);
                }
            }
        });

    }

    /**
     * 发送测试
     */
    private void onSendTestClick() {
        final String ip = ipEt.getText().toString().trim();
        final String port = portEt.getText().toString().trim();
        final String testMsg = sendTestEt.getText().toString().trim();
        if(TextUtils.isEmpty(ip)
                || TextUtils.isEmpty(port)
                || TextUtils.isEmpty(testMsg)) {

            toast("ip、端口号 以及要发送的内容都不能为空哦");
            return;
        }

        SocketClient.getSingle().sendMsg(ip, Integer.valueOf(port), testMsg, new ISendMessageListener() {
            @Override
            public void sendProgress(boolean isSendSuccess, String curSendCmd, String reason) {
                LogUtil.thduan("onSendTestClick sendProgress : " + isSendSuccess + ", " + reason);
                logEt.append("\n" + reason);
            }

            @Override
            public void sendComplete(boolean isAllSuccess, String reason) {

            }
        });
    }

    /**
     * 全开
     */
    private void onOpenAllClick() {
        if(checkiPPort()) {
            final String ip = ipEt.getText().toString().trim();
            final String port = portEt.getText().toString().trim();

            logEt.append("\n-------------- 全开 ------------------");
            openAllBtn.setEnabled(false);

            SocketClient.getSingle().openAllCmd(ip, Integer.valueOf(port), new ISendMessageListener() {
                @Override
                public void sendProgress(boolean isSendSuccess, String curSendCmd, String reason) {
                    logEt.append("\n命令 " + curSendCmd + " " + reason);
                }

                @Override
                public void sendComplete(boolean isAllSuccess, String reason) {
                    openAllBtn.setEnabled(true);
                    if(isAllSuccess) {
                        logEt.append("\n全开命令发送成功");
                    }else {
                        logEt.append("\n全开命令发送失败");
                    }
                }
            });
        }
    }

    /**
     * 全关
     */
    private void onCloesAllClick() {
        if(checkiPPort()) {
            final String ip = ipEt.getText().toString().trim();
            final String port = portEt.getText().toString().trim();

            logEt.append("\n-------------- 全关 ------------------");
            closeAllBtn.setEnabled(false);

            SocketClient.getSingle().closeAllCmd(ip, Integer.valueOf(port), new ISendMessageListener() {
                @Override
                public void sendProgress(boolean isSendSuccess, String curSendCmd, String reason) {
                    logEt.append("\n命令 " + curSendCmd + " " + reason);
                }

                @Override
                public void sendComplete(boolean isAllSuccess, String reason) {
                    closeAllBtn.setEnabled(true);
                    if(isAllSuccess) {
                        logEt.append("\n全关命令发送成功");
                    }else {
                        logEt.append("\n全关命令发送失败");
                    }
                }
            });
        }
    }

    /**
     * 检测ip、端口是否为空
     * @return
     */
    private boolean checkiPPort() {
        final String ip = ipEt.getText().toString().trim();
        final String port = portEt.getText().toString().trim();
        if(TextUtils.isEmpty(ip)
                || TextUtils.isEmpty(port)) {
            toast("ip 、 端口号不能为空");
            return false;
        }else {
            return true;
        }
    }

    private void toast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}
